/// <reference path="../_all.ts" />

module Application.Controllers {

    export class ServersController implements Application.Interfaces.Controllers.IController {

        public static $inject = [
            '$rootScope',
            '$scope',
            '$location',
            'ServerService',
            'TranslationService',
        ];

        serversResult: {
            (data): void;
            (data, err): void;
        };

        constructor(public RootScope: Application.Interfaces.Scopes.IRootScope
            , public Scope: Application.Interfaces.Scopes.IServersScope
            , public Location: ng.ILocationService
            , public Servers: Application.Interfaces.Services.IServerService
            , public Translations: Application.Interfaces.Services.ITranslationService) {
            this.Scope.vm = this;
            this.Scope.user = this.RootScope.user;
            this.Scope.location = this.Location;
            this.Scope.messages = new Application.Utility.Messages();
            this.Scope.navigations = new Application.Utility.Navigations();
            this.Scope.servers = <Application.Resources.IServer[]>[];

            this.Scope.navigations.AddLink(Translations.Translate("SERVERLIST"), "/servers", true, false,Application.Constants.Icons.Servers);
            this.Scope.navigations.AddLink(Translations.Translate("ACTIONS"), "/actions", false, false, Application.Constants.Icons.Actions);
            if (this.Scope.user && this.Scope.user.role && this.Scope.user.role >= 2) {
                this.Scope.navigations.AddLink(Translations.Translate("PLAYERS"), "/players", false, false, Application.Constants.Icons.Bans);
            };
            if (this.Scope.user && this.Scope.user.role && this.Scope.user.role >= 8) {
                this.Scope.navigations.AddLink(Translations.Translate("USERS"), "/users", false, false, Application.Constants.Icons.Users);
            };
            this.Scope.navigations.AddLinkRight(Translations.Translate("ACCOUNT"), "/users/" + this.Scope.user.user_id, false);
            this.Scope.navigations.AddLinkRight(Translations.Translate("LOGOUT"), "/", false);

            this.serversResult = (data, err?) => {
                this.serversResultProxy.apply(this, [data, err]);
            }

            this.getServers();
        }

        public log(msg: string) {
            //console.log(msg);
        }

        public getServers() {
            this.log('getServers');
            this.Servers.GetServers(this.serversResult);
        }

        private serversResultProxy(data, err) {
            this.log('server result');
            if (data != null) {
                var servers = <Application.Resources.IServer[]> data;
                if (servers != null && servers.length > 0) {
                    this.Scope.messages.AddAndGrowl(Application.Utility.MessageType.info, "Info", new Application.Utility.String(this.Translations.Translate('SERVERSFOUND')).format(servers.length.toString()));
                    for (var i = 0; i < this.Scope.servers.length; i++) {
                        var result: Application.Resources.IServer;
                        for (var j = 0; j < servers.length; j++) {
                            if (this.Scope.servers[i].server_id == servers[j].server_id) {
                                result = servers[j];
                                Application.Resources.ServerUtility.fixBooleans(result);
                                servers.splice(j, 1);
                                break;
                            };
                        };
                        if (result == null) {
                            this.Scope.servers.splice(i, 1);
                            i--;
                        } else {
                            this.Scope.servers[i] = result;
                        };
                    }; 
                    for (var i = 0; i < servers.length; i++) {
                        var result = servers[i];
                        Application.Resources.ServerUtility.fixBooleans(result);
                        this.Scope.servers.push(result);
                    };
                } else {
                    this.Scope.messages.AddAndGrowl(Application.Utility.MessageType.error, 'Error', this.Translations.Translate('SERVERSNOTFOUND'));
                };
            } else {
                if (err != null) {
                    this.Scope.messages.AddAndGrowl(Application.Utility.MessageType.error, err.data.status + ' ' + err.data.type, err.data.message);
                } else {
                    this.Scope.messages.AddAndGrowl(Application.Utility.MessageType.error, '000 Unknown', this.Translations.Translate('UNKNOWNERROR'));
                };
            };
        }
    };
}